//
//  ESVNavigator+Extension.swift
//  ESVNavigator
//
//  Created by Vencent on 2020/11/12.
//

import Foundation
import URLNavigator

extension UIViewController {
    public var navigator: ESVNavigator { .default }
}

public extension NavigatorType {
    func register(_ pattern: Map, _ factory: @escaping ViewControllerFactory) {
        self.register(pattern.path, factory)
    }
    
    func handle(_ pattern: Map, _ factory: @escaping URLOpenHandlerFactory) {
        self.handle(pattern.path, factory)
    }
    
    func viewController(for url: Map, context: Any? = nil) -> UIViewController? {
        return self.viewController(for: url.path, context: context)
    }

    func handler(for url: Map, context: Any? = nil) -> URLOpenHandler? {
        return self.handler(for: url.path, context: context)
    }
    
    @discardableResult
    func push(_ url: Map, context: Any? = nil, from: UINavigationControllerType? = nil, animated: Bool = true) -> UIViewController? {
        return self.pushURL(url.path, context: context, from: from, animated: animated)
    }

    @discardableResult
    func present(_ url: Map, context: Any? = nil, wrap: UINavigationController.Type? = nil, from: UIViewControllerType? = nil, animated: Bool = true, completion: (() -> Void)? = nil) -> UIViewController? {
        return self.presentURL(url.path, context: context, wrap: wrap, from: from, animated: animated, completion: completion)
    }
    
    @discardableResult
    func open(_ url: Map, context: Any? = nil) -> Bool {
        return self.openURL(url.path, context: context)
    }
}
